package com.powerManager.sdk.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.log4j.Logger;

import com.powerManager.sdk.common.InitSystem;
import com.powerManager.sdk.utils.SysUtils;

/** -- 接收页面功能请求,分配请求到功能处理器.
 * Servlet implementation class MainServlet
 */
public final class MainServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private final Logger LOG = Logger.getLogger(MainServlet.class);
    public static String root = null;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MainServlet() {
        super();
    }
    
    //初始化页面数据
    public void init(ServletConfig config) throws ServletException {
    	root = SysUtils.read("base", "site");
    	config.getServletContext().setAttribute("root", root);
    	InitAppPathMap.reloadPath();
    	InitSystem.loadMenu();
    	InitSystem.loadButton();
    	InitSystem.loadPrivilege();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//http://host:port/app/PathID!target-paramates
		this.doProcess(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doProcess(request,response);
	}
	
	/** -- 页面请求分配
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//请求路径
		String path = request.getPathInfo();
		//页面路径中的分解参数
		String params = null;
		
		LOG.info(path);
		if(path.equals("/") || path == null){
			response.sendRedirect("/404.jsp");
			return;
		}
		params = path.replaceAll(".*!", "");
		path = path.replaceAll("!.*", "");
		RequestProcess rp = new RequestProcess();
		System.out.println(params);
		String return_value = rp.doProcess(request, response, path, params.split("\\-"));
		this.doReturn(return_value, request, response);
	}
	
	private void doReturn(String value, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		//页面处理完成后的页面转发
		if(value == null){
			response.sendRedirect("/500.jsp");
		}else if(value.indexOf("path:")==0){
			request.getRequestDispatcher(value.replaceFirst("path:", "")).forward(request, response);
		}else if (value.indexOf("jsp:")==0){
			response.sendRedirect(value.replaceFirst("jsp:", root));
		}else if(value.indexOf("text:")==0){
			PrintWriter out = response.getWriter();
			out.print(value.replaceFirst("text:", ""));
			out.flush();
			out.close();
		}
	}

}
